<?php
//手机端判断可领取的

function filegetcard($card_id,$openid){
    //用户已领数量
    $CardUserGet = new \app\common\model\CardUserGet();
    $sume = $CardUserGet->where(['card_id' => $card_id, 'openid' => $openid])->count();
    //可领数量
    $CardGeneral = new \app\common\model\CardGeneral();
    $cards = $CardGeneral->where(['card_id' => $card_id])->field('get_limit,end_timestamp,stocks,consume,time_type,title,logo_url,brand_name,card_type')->find();
//    file_put_contents('/data2/html/payos/filecards.txt', $card_id.'-file:' . json_encode($sume.'-'.$cards['get_limit']).PHP_EOL, FILE_APPEND);
    //是否可领
    if ($cards['get_limit'] > $sume){
        //是否过期
        if ($cards['time_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
            $time = time();
            if ($cards['end_timestamp'] < $time) {
                $res['result'] = 2;     //已过期
                $res['cards'] = $cards;
            } else {
                //是否库存
                if ($cards['stocks'] > 0) {
                    $res['result'] = 1;
                    $res['cards'] = $cards;
                } else {
                    $res['result'] = 2;     //无库存
                    $res['cards'] = $cards;
                }
            }
        } else {
            $res['result'] = 1;
            $res['cards'] = $cards;
        }
    }else{
        $res['result'] = 4;         //不可领
        $res['cards'] = $cards;
    }
//    file_put_contents('/data2/html/payos/filecards.txt', $card_id.'-file-1:' . json_encode($res).PHP_EOL, FILE_APPEND);
    return $res;
}

//手机端判断自己已领取的
function filemegetcard($card_id,$code,$create_time){
    $CardGel = new \app\common\model\CardGeneral();
    $cards = $CardGel->where(['card_id'=>$card_id])->field('end_timestamp,stocks,consume,time_type,fixed_term,title,logo_url,brand_name')->find();
    $time = time();
    if ($cards['time_type'] == 'DATE_TYPE_FIX_TERM') {
//        $CardUserGet = new \app\common\model\CardUserGet();
//        $user = $CardUserGet->where(['card_id' => $card_id, 'code' => $code])->field('create_time,have,statu')->find();
        $end_time = strtotime($create_time) + 86400 * $cards['fixed_term'];
        if ($end_time < $time) {
            $res['result'] = 2;
            $res['cards'] = $cards;
        } else {
            $res['result'] = 1;
            $res['cards'] = $cards;
        }
    } else {
        if ($cards['end_timestamp'] < $time) {
            $res['result'] = 2;
            $res['cards'] = $cards;
        } else {
            $res['result'] = 1;
            $res['cards'] = $cards;
        }
    }
//    file_put_contents('/data2/html/payos/filecards.txt', $card_id.'-file-2:' . json_encode($res).PHP_EOL, FILE_APPEND);
    return $res;
}

//过滤过期
//function filecardgq($card){
//    if ($card['time_type'] == 'DATE_TYPE_FIX_TIME_RANGE') {
//        $time = time();
//        if ($card['end_timestamp'] < $time) {
//            $res = false;
//        } else {
//            if ($card['stocks'] > 0) {
//                $res = true;
//            } else {
//                $res = false;
//            }
//        }
//    } else {
//        $res = true;
//    }
//    return $res;
//}